package com.ggk911.pharmacy_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ggk911.pharmacy_system.entity.DTO.InventoryDTO;
import com.ggk911.pharmacy_system.entity.Inventory;
import com.ggk911.pharmacy_system.mapper.InventoryMapper;
import com.ggk911.pharmacy_system.service.IInventoryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ggk-911
 * @version 1.0 2023/4/11 17:12
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements IInventoryService {

    /**
     * page的增强
     *
     * @param page
     * @param queryWrapper
     * @param <E>
     * @return
     */
    public <E extends IPage<Inventory>> E pageAd(E page, Wrapper<Inventory> queryWrapper) {
        return baseMapper.selectPageAd(page, queryWrapper);
    }

    /**
     * 重写的save去重
     *
     * @param entity
     * @return
     */
    @Override
    public boolean save(Inventory entity) {
        Inventory inventory = baseMapper.selectOneByDrugInfoId(entity.getDrugInfoId());
        if (inventory != null) {
            return baseMapper.updateByNoRepeat(entity.getInventoryNum() + inventory.getInventoryNum(), entity.getDrugInfoId());
        }
        return baseMapper.insertAd(entity.getInventoryNum(), entity.getDrugInfoId());
    }

    /**
     * listAd
     *
     * @param queryWrapper
     * @return
     */
    public List<Inventory> listAd(QueryWrapper<Inventory> queryWrapper) {
        return baseMapper.selectListAd(queryWrapper);
    }

    /**
     * selectSumByType
     *
     * @return
     */
    public List<InventoryDTO> selectSumByType() {
        return baseMapper.selectSumByType();
    }

    /**
     * 获取库存预警
     */
    public Integer selectInventoryLimit() {
        return baseMapper.selectInventoryLimit();
    }

    /**
     * 修改limit
     *
     * @return
     */
    public boolean updateInventoryLimit(Integer limit) {
        return baseMapper.updateInventoryLimit(limit);
    }
}
